/* Exercise 6.1 Random number workout */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #include "lcdlib.h" unsigned char seed = 55 ; //Variables associated with random number generation unsigned char mask = 1 ; //LFSR based algorithm for random number generation //exclusive or bit 4 with bit 7, shift the seed and add the new bit unsigned char random ( void ) { unsigned char top, mid ; if ( seed & mask ) //get new input bit { mid = 1 ; } else { mid = 0 ; } if ( seed & 0x80 ) { top = 1 ; } else { top = 0 ; } seed = ( seed << 1 ) | ( mid ^ top ) ; return seed ; } void display_value ( int value ) { unsigned char hunds, tens, units ; //First get the digits units = value % 10; value = value / 10; tens = value % 10; value = value / 10; hunds = value % 10; //Now display them lcd_print_ch ('0' + hunds); lcd_print_ch ('0' + tens); lcd_print_ch ('0' + units); } int main (void) { int cyclesize = 0; //counter variables int cyclemax = 0; int i; int winstart = 0; //winning values int winmask = 0; ANSELB = 0x00; //Put PORTB into Digital mode, defaults to analogue OPTION_REG = 0xC0; lcd_start (); for (mask = 1; mask < 128; mask = mask + 1 ) //for each possible mask value { lcd_cursor ( 0, 0 ); display_value ( mask ); //show what we are working on for ( i=0 ; i < 255 ; i++ ) //try each seed in turn { seed = i; cyclesize = 0; //clear the cycle counter while (random () != i) //get random numbers until we end up back where we started { cyclesize++; } if (cyclesize > cyclemax) //if we have a new largest cycle we store the values which created it { winstart = i; winmask = mask; cyclemax = cyclesize; } } } lcd_cursor (0, 0); display_value (winmask); //display the results lcd_print_ch ( ' ' ); display_value (winstart); lcd_print_ch ( ' ' ); display_value (cyclemax); while (1) //Loop forever { } return 0; }